//
//  OneselfCentreViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/11.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "OneselfCentreViewController.h"
#import "RealNameCertifyView.h"
#import "CountryModel.h"
@interface OneselfCentreViewController ()
@property (weak, nonatomic) IBOutlet UILabel *udidLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIView *countryView;
@property (weak, nonatomic) IBOutlet UIView *nameView;
@property (weak, nonatomic) IBOutlet UIView *numberView;
@property (weak, nonatomic) IBOutlet UIButton *certifyBtn;
@property (weak, nonatomic) IBOutlet UILabel *certificateCountryLabel;
@property (weak, nonatomic) IBOutlet UILabel *fullnameLabel;
@property (weak, nonatomic) IBOutlet UILabel *certificateNumLabel;

@end

@implementation OneselfCentreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSMutableString *phoneStr1 = [NSMutableString stringWithString:self.phone];
    if (phoneStr1.length>4) {
        [phoneStr1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"****"];
    }
    self.phoneLabel.text=phoneStr1;
    
    self.udidLabel.text=self.udid;
    
    if ([self.status isEqualToString:@"1"]) {
        self.certifyBtn.hidden=YES;
        self.countryView.hidden=NO;
        self.nameView.hidden=NO;
        self.numberView.hidden=NO;
        
    }else{
        self.certifyBtn.hidden=NO;
        self.countryView.hidden=YES;
        self.nameView.hidden=YES;
        self.numberView.hidden=YES;
    }
    
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
    [self.phoneLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [self requstData];
   
}
-(void)requstData{
    [LSRequestManager getRequestWithURL:HUOBI(@"api/user/info")
                                 params:nil
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        self.certificateCountryLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"user_auth"][@"certificate_country_id"]];
                                        self.fullnameLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"user_auth"][@"fullname"]];
                                        self.certificateNumLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"user_auth"][@"certificate_num"]];
                                        
                                        
                                    } else {
                                        self.certificateCountryLabel.text=@"";
                                        self.fullnameLabel.text=@"";
                                        self.certificateNumLabel.text=@"";
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    self.certificateCountryLabel.text=@"";
                                    self.fullnameLabel.text=@"";
                                    self.certificateNumLabel.text=@"";
                                }
                                showHUD:NO];
}


- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)realnameCertifyAction:(UIButton *)sender {
    
    [LSRequestManager getRequestWithURL:HUOBI(@"api/pub/getCountry")
                                 params:nil
                                success:^(NSDictionary *responseData) {
                                    if ([responseData[@"ret"] integerValue ] == 0) {
                                        NSArray *array = [CountryModel objectArrayWithKeyValuesArray:responseData[@"data"]];
                                        
                                        NSMutableArray *frameArray = [NSMutableArray array];
                                        
                                        for (CountryModel *countryModel in array) {
                                            [frameArray addObject:countryModel.label];
                                        }
                                        RealNameCertifyView *realNameCertifyView = [RealNameCertifyView xib];
                                        realNameCertifyView.countryView.listArray=[frameArray copy];
                                        [[UIApplication sharedApplication].keyWindow addSubview:realNameCertifyView];
                                        __weak typeof(realNameCertifyView) weakRealNameCertifyView = realNameCertifyView;
                                        realNameCertifyView.submitActionBlock = ^(NSInteger tag) {
                                            NSLog(@"coutry------%@",weakRealNameCertifyView.countryView.text);
                                            
                                            NSString*countryName;
                                            for (CountryModel *countryModel in array) {
                                                if ([weakRealNameCertifyView.countryView.text isEqualToString:countryModel.label]) {
                                                    countryName=countryModel.value;
                                                }
                                            }
                                            
                                            NSDictionary *param = @{@"fullname" : weakRealNameCertifyView.nameTF.text?weakRealNameCertifyView.nameTF.text:@"",
                                                                    @"certificate_country_id" : countryName?countryName:@"",
                                                                    @"certificate_num" : weakRealNameCertifyView.numberTF.text?weakRealNameCertifyView.numberTF.text:@"",
                                                                    };
                                            [weakRealNameCertifyView removeFromSuperview];
                                            [LSRequestManager putRequestWithURL:HUOBI(@"api/user/authRealname")
                                                                         params:param
                                                                        success:^(NSDictionary *responseData) {
                                                                            if ([responseData[@"ret"] integerValue ] == 0) {
                                                                                self.certificateCountryLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"certificate_country_id"]];
                                                                                self.fullnameLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"fullname"]];
                                                                                self.certificateNumLabel.text=[NSString stringWithFormat:@"%@",responseData[@"data"][@"certificate_num"]];
                                                                                self.certifyBtn.hidden=YES;
                                                                                self.countryView.hidden=NO;
                                                                                self.nameView.hidden=NO;
                                                                                self.numberView.hidden=NO;
                                                                                
                                                                            } else {
                                                                                
                                                                                self.certificateCountryLabel.text=@"";
                                                                                self.fullnameLabel.text=@"";
                                                                                self.certificateNumLabel.text=@"";
                                                                                [MBProgressHUD showError:responseData[@"msg"]];
                                                                            }
                                                                        }
                                                                        failure:^(NSError *error) {
                                                                            self.certificateCountryLabel.text=@"";
                                                                            self.fullnameLabel.text=@"";
                                                                            self.certificateNumLabel.text=@"";
                                                                        }
                                                                        showHUD:NO];
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                        };
                                        
                                    } else {
                                        [MBProgressHUD showError:responseData[@"msg"]];
                                    }
                                }
                                failure:^(NSError *error) {
                                    
                                }
                                showHUD:NO];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}



@end
